package net.chenlin.dp.modules.newBusiness.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.exception.RRException;
import net.chenlin.dp.common.support.redis.RedisCacheManager;
import net.chenlin.dp.modules.newBusiness.entity.dto.DistrictTopologyDto;
import net.chenlin.dp.modules.newBusiness.entity.dto.NewDistrictDto;
import net.chenlin.dp.modules.newBusiness.entity.vo.DeviceUsersDataVo;
import net.chenlin.dp.modules.newBusiness.entity.vo.NewDistrictListVo;
import net.chenlin.dp.modules.newBusiness.service.NewDistrictManageService;
import net.chenlin.dp.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: Yansgm
 * @Description:
 * @Date: 2025-04-01 17:51:01
 */
@Slf4j
@RestController
@RequestMapping("/newDistrict")
@Api(tags = "ysm-新版-台区管理")
public class NewDistrictManageController {

    @Autowired
    private NewDistrictManageService service;

    @Autowired
    private RedisCacheManager redisCacheManager;

    @RequestMapping(value = "/getAllDistrict", method = RequestMethod.GET)
    @ApiOperation(value = "首页")
    public R getAllDistrict(String name) {
        return service.getAllDistrict(name);
    }

    @RequestMapping(value = "/getDistrictList", method = RequestMethod.POST)
    @ApiOperation(value = "台区列表")
    public Page<NewDistrictListVo> getDistrictList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token != null) {
            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
            if(loginUser == null){
                throw new RRException("未获取到当前登录的用户信息!");
            }
            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
            if(!loginUser.getUsername().equals("admin")){
                params.put("userId",loginUser.getUserId());
            }
        }
        return service.getNewDistrictList(params);
    }

    @RequestMapping(value = "/creatDistrict", method = RequestMethod.POST)
    @ApiOperation(value = "创建台区")
    public R creatDistrict(@RequestBody NewDistrictDto dto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token != null) {
            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
            if(loginUser != null){
                dto.setCreateId(loginUser.getUserId());
            }
            return service.creatDistrict(dto);
        }else{
            return R.error("Token为空，请联系管理员！");
        }
    }

    @RequestMapping(value = "/editDistrict", method = RequestMethod.POST)
    @ApiOperation(value = "编辑台区")
    public R editDistrict(@RequestBody NewDistrictDto dto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token != null) {
            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
            if(loginUser != null){
                dto.setEditId(loginUser.getUserId());
            }
            return service.editDistrict(dto);
        }else{
            return R.error("Token为空，请联系管理员！");
        }
    }

    @RequestMapping(value = "/deleteDistrict", method = RequestMethod.GET)
    @ApiOperation(value = "删除台区")
    public R deleteDistrict(Integer id) {
//        String token = request.getHeader("Authorization");
//        if (token != null) {
//            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
//            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
//            return service.deleteDistrict(id);
//        }
        return service.deleteDistrict(id);
    }

    @RequestMapping(value = "/getDistrictOnline", method = RequestMethod.GET)
    @ApiOperation(value = "查询台区是否还有设备在线")
    public R getDistrictOnline(Integer id) {
        return service.getDistrictOnline(id);
    }

    @RequestMapping(value = "/getTopologyList", method = RequestMethod.POST)
    @ApiOperation(value = "拓扑节点列表")
    public R getTopologyList(Integer districtId) {
        return service.getTopologyList(districtId);
    }

    @RequestMapping(value = "/creatTopologyNode", method = RequestMethod.POST)
    @ApiOperation(value = "新增拓扑节点")
    public R creatTopologyNode(@RequestBody DistrictTopologyDto dto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token != null) {
            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
            if(loginUser != null){
                dto.setCreateId(loginUser.getUserId());
            }
            return service.creatTopologyNode(dto);
        }else{
            return R.error("Token为空，请联系管理员！");
        }
    }

    @RequestMapping(value = "/editTopologyNode", method = RequestMethod.POST)
    @ApiOperation(value = "编辑拓扑节点")
    public R editTopologyNode(@RequestBody DistrictTopologyDto dto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println(token);
        if (token != null) {
            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
            if(loginUser != null){
                dto.setEditId(loginUser.getUserId());
            }
            return service.editTopologyNode(dto);
        }else{
            return R.error("Token为空，请联系管理员！");
        }
    }

    @RequestMapping(value = "/deleteTopologyNode", method = RequestMethod.GET)
    @ApiOperation(value = "删除拓扑节点")
    public R deleteTopologyNode(Integer nodeId) {
//        String token = request.getHeader("Authorization");
//        if (token != null) {
//            SysUserEntity loginUser = (SysUserEntity) redisCacheManager.get(token);
//            System.out.println("监测设备日统计数据的 登录 user ===== "+loginUser);
//            if(CollectionUtils.isNotEmpty(loginUser.getRoleIdList())){
//                List<Long> roleIdList = loginUser.getRoleIdList();
//                // 主账号才可以创建台区，配置监测点位
//                if(roleIdList.contains(3)){
//                    return service.deleteTopologyNode(nodeId);
//                }
//            }
//        }
        return service.deleteTopologyNode(nodeId);
    }

    @RequestMapping(value = "/getNextTopologNode", method = RequestMethod.GET)
    @ApiOperation(value = "获取台区拓扑图下级节点")
    public R getNextTopologNode(Integer nodeId) {
        return service.getNextTopologNode(nodeId);
    }

    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取台区树")
    public R getTree(Integer districtId) {
        return service.getTree(districtId);
    }

    @RequestMapping(value = "/getDistrictInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取台区信息")
    public R getDistrictInfo(Integer id) {
        return service.getDistrictInfo(id);
    }

    @Transactional
    @ApiOperation(value = "导入设备与户表的对应关系")
    @PostMapping("/importDeviceUserExcel")
    public R importDeviceUserExcel(@RequestParam("file") MultipartFile file, @RequestParam("districtId") Long districtId) throws Exception {
        //批量导入案例
        service.deviceExcel(file,districtId);
        return R.ok("导入成功");
    }

    @Transactional
    @ApiOperation(value = "导入用户数据")
    @PostMapping("/importUserData")
    public R importUserData(@RequestParam("file") MultipartFile file, @RequestParam("districtId") Long districtId) throws Exception {
        //批量导入案例
        service.importUserData(file,districtId);
        return R.ok("导入成功");
    }

    @RequestMapping(value = "/getDeviceUserList", method = RequestMethod.POST)
    @ApiOperation(value = "设备与户表的对应关系列表")
    public Page<DeviceUsersDataVo> getDeviceUserList(@RequestBody Map<String, Object> params) {
        return service.getDeviceUserList(params);
    }


}
